package com.celan.year2023.month09.day29;

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if (n == 0) return true;
        if (flowerbed.length == 1) {
            if (n > 1) return false;
            else return flowerbed[0] == 0;
        }
        // 每个位置判断是否能够插入
        for (int i = 0; i < flowerbed.length; i++) {
            if (flowerbed[i] == 1) continue;
            boolean flag = true;
            if (i == 0) {
                flag = flowerbed[i + 1] == 0;
            } else if (i == flowerbed.length - 1) {
                flag = flowerbed[i - 1] == 0;
            } else {
                flag = flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0;
            }
            if (flag) {
                flowerbed[i] = 1;
                n--;
                if (n == 0) return true;
            }
        }
        return n <= 0;
    }
}